<?php
namespace app\model;

use think\model\relation\BelongsTo;

/*
 *  vip服务
 */
class UserVip extends \think\Model
{
    protected $table = 'user_vip';      // 工作人员
    /**
     * 自动时间戳数据类型
     *
     * @var string
     */
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'create_at';
    protected $updateTime = false;

    protected $field = ['user_id', 'level', 'status', 'delete_at']; 
    // 定义全局的查询范围
    protected $globalScope = ['user'];
    // 查询范围
    public function scopeUser($query)
    {
        if(request()->subIn(['m', 'mini', 'mobile', 'app'])){
            $query->where('user_id', request()->uid);
        }
    }
    /**
     * 用户
     *
     * @return void
     */
    public function user() {
        return $this->belongsTo('User');
    }
    public function searchUserIdAttr($query, $value, $data)
    {
        $value > 0 && $query->where('user_id', $value);
    }
    /*
     * 新增前
     */
    public static function onBeforeInsert($model)
    {
        $model->user_id = request()->uid;
    }
}
